import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-11-05
 * Time: 20:17
 */
public class Test {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();

        ArrayList<String> list = new ArrayList<>();

        LinkedList<String> linkedList2 = new LinkedList<>(list);
    }
    public static void main6(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(2);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(4);
        myLinkedList.addFirst(5);
        myLinkedList.addFirst(5);

        myLinkedList.display();
      /*  myLinkedList.addIndex(20,99);
        myLinkedList.display();*/
        System.out.println("===================");
        myLinkedList.removeAllKey(5);
        myLinkedList.display();
        System.out.println("清空操作");
        myLinkedList.clear();
        myLinkedList.display();

    }
    public static MySingleList.ListNode mergeTwoLists
            (MySingleList.ListNode head1,MySingleList.ListNode head2){
        MySingleList.ListNode newHead = new MySingleList.ListNode(0);
       MySingleList.ListNode tmp = newHead;
        while (head1 != null && head2 != null){
            if(head1.val < head2.val){
                tmp.next = head1;
                head1 = head1.next;
                tmp = tmp.next;
            }else {
                tmp.next = head2;
                head2 = head2.next;
                tmp = tmp.next;
            }
        }
        if(head1 != null){
            tmp.next = head1;
        }
        if(head2 != null){
            tmp.next = head2;
        }
        return newHead.next;
    }
    public static MySingleList.ListNode getIntersectionNode
            (MySingleList.ListNode headA,MySingleList.ListNode headB){
        //1.分别求两个链表的长度
        int lenA = 0;
        int lenB = 0;
        MySingleList.ListNode pl = headA;
        MySingleList.ListNode ps = headB;

        while (pl != null){
            lenA++;
            pl = pl.next;
        }
        while(ps != null){
            lenB++;
            ps = ps.next;
        }
        //求完长度之后将pl和ps指回来
        pl = headA;
        ps = headB;
        int len = lenA - lenB;
        //根据len的值，修改指向
        if(len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //1.可以保证len一定是一个正数
        //2.pl指向的链表一定是最长的，ps指向的链表一定是最短的
        while (len != 0){
            pl = pl.next;
            len -- ;
        }
        while (pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        //pl == ps
        if(pl == ps && pl == null){
            return null;
        }
        return pl;
    }
    public static void createCut(MySingleList.ListNode headA,MySingleList.ListNode headB){
        headB.next.next = headA.next.next;
    }

    public static void main5(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(21);
        mySingleList.addLast(31);
        mySingleList.addLast(41);
        mySingleList.display();
        mySingleList.createLoop();

        boolean flg = mySingleList.hasCycle();
        System.out.println(flg);
        MySingleList.ListNode ret = mySingleList.detectCycle();
        System.out.println(ret.val);
    }

    public static void main4(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(21);
        mySingleList.addLast(31);
        mySingleList.addLast(41);
        mySingleList.display();
        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(8);
        mySingleList2.addLast(10);
        mySingleList2.addLast(35);
        mySingleList2.addLast(38);
        mySingleList2.display();

        System.out.println("==============");
      /*  MySingleList.ListNode ret =
                mergeTwoLists(mySingleList.head,mySingleList2.head);
        mySingleList2.display(ret);*/
        createCut(mySingleList.head,mySingleList2.head);
        MySingleList.ListNode ret = getIntersectionNode(mySingleList.head,mySingleList2.head);
        System.out.println(ret.val);
    }



    public static void main3(String[] args) {
        MySingleList mySingleList = new MySingleList();
        System.out.println("========测试尾插========");
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(3);
        mySingleList.display();
        System.out.println("=======");
        //ListNode是一个内部类，类型需要使用【外部类类名.内部类】的方式拿到.
//        MySingleList.ListNode ret = mySingleList.reverseList();
//        mySingleList.display(ret.next);
        MySingleList.ListNode ret = mySingleList.findKthToTail(2);
        System.out.println(ret.val);
    }
    public static void main2(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.createLink();
        mySingleList.display();
        System.out.println(mySingleList.contains(4));
        System.out.println(mySingleList.size());
        System.out.println("========测试尾插========");
        mySingleList.addLast(1);
        mySingleList.addLast(1);
        mySingleList.display();
        System.out.println("========测试头插=========");
        mySingleList.addFirst(1);
        mySingleList.addFirst(1);
        mySingleList.addFirst(1);
        mySingleList.display();
        System.out.println("=========测试插入==========");
        mySingleList.addIndex(0,1);
        try{
            mySingleList.addIndex(1,1);
        }catch (ListIndexOutOfException e){
            e.printStackTrace();
        }
        mySingleList.display();
        System.out.println("=====删除一个节点======");
        mySingleList.remove(1);
        mySingleList.display();
        mySingleList.remove(1);
        mySingleList.display();
        System.out.println("=====删除全部节点======");
        mySingleList.removeAllKey(1);
        mySingleList.display();


    }
}
